export const formatTime = (date, format = "", flg = '') => {
	const year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	let hour = date.getHours();
	let minute = date.getMinutes();
	const second = date.getSeconds();
	let weekIndex = date.getDay(),
		week_day = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];

	if (month < 10) {
		month = "0" + month;
	}
	if (day < 10) {
		day = "0" + day;
	}
	if (format == 'week') {
		const today = new Date();
  const year1 = today.getFullYear();
  const month1 = String(today.getMonth() + 1).padStart(2, '0');
  const day1 = String(today.getDate()).padStart(2, '0');
	const d = `${year}-${month}-${day}`
  const formattedToday = `${year1}-${month1}-${day1}`;
		if (formattedToday === d) {
			return '今天';
		} else if (year===year1&&month===month1&&day1-day==1) {
			return '昨天';
		}
		return week_day[weekIndex]
	}
	if (format === "y-m-d") {
		return `${year}-${month}-${day}`;
	}

	if (format === "m-d") {
		return `${month}月${day}日`;
	}
	if (format === "h:m") {
		minute = minute < 10 ? "0" + minute : minute;
		hour = hour < 10 ? "0" + hour : hour;
		return `${hour}:${minute}`;
	}
	if (format === "last") {
		const t = new Date(year, date.getMonth(), 1).getDate();
		const s = new Date(year, date.getMonth() + 1, 0).getDate();
		return {
			start: [year, month, t].map(formatNumber).join(""),
			end: [year, month, s].map(formatNumber).join(""),
		};
	}
	if (format === "y-m") {
		return `${year}年${month}日`;
	}
	return [year, month, day].map(formatNumber).join(flg);
};

const formatNumber = (n) => {
	n = n.toString();
	return n[1] ? n : "0" + n;
};

//相加
export const addCom = function(arg1, arg2, d) {
	arg1 = arg1.toString(), arg2 = arg2.toString();
	var arg1Arr = arg1.split("."),
		arg2Arr = arg2.split("."),
		d1 = arg1Arr.length == 2 ? arg1Arr[1] : "",
		d2 = arg2Arr.length == 2 ? arg2Arr[1] : "";
	var maxLen = Math.max(d1.length, d2.length);
	var m = Math.pow(10, maxLen);
	var result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen));
	var d = arguments[2];
	return typeof d === "number" ? Number((result).toFixed(d)) : result;
}
// 两个数之间相减
export const reduceCom = function(arg1, arg2) {
	var r1, r2, m, n;
	try {
		r1 = arg1.toString().split(".")[1].length
	} catch (e) {
		r1 = 0
	};
	try {
		r2 = arg2.toString().split(".")[1].length
	} catch (e) {
		r2 = 0
	};
	m = Math.pow(10, Math.max(r1, r2));
	// 动态控制精度长度
	n = (r1 >= r2) ? r1 : r2;
	return ((arg1 * m - arg2 * m) / m).toFixed(n);
}
//两个数乘法，精准计算
export const accMul = function(arg1, arg2) {
	var m = 0,
		s1 = arg1.toString(),
		s2 = arg2.toString();
	try {
		m += s1.split(".")[1].length
	} catch (e) {};
	try {
		m += s2.split(".")[1].length
	} catch (e) {};
	return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}
// 除法
export const div = (arg1, arg2) => {
	if (!arg1) return '';
	if (!arg2) return '';
	var t1 = 0,
		t2 = 0,
		r1, r2;
	try {
		t1 = arg1.toString().split(".")[1].length
	} catch (e) {}
	try {
		t2 = arg2.toString().split(".")[1].length
	} catch (e) {}
	r1 = Number(arg1.toString().replace(".", ""));
	r2 = Number(arg2.toString().replace(".", ""));
	return (r1 / r2) * Math.pow(10, t2 - t1);
}